package leetcode;

import leetcode.model.TreeNode;
import org.junit.Test;

/**
 * @author: feiwang_6
 * @create: 2020/7/19 10:51
 * @description:
 */
public class 二叉树遍历 {
    
    @Test
    public void testIterators(){
        TreeNode<Integer> root = new TreeNode<Integer>(10);
        TreeNode<Integer> left = new TreeNode<Integer>(6);
        TreeNode<Integer> right = new TreeNode<Integer>(12);
        root.left = left;
        root.right = right;

        TreeNode<Integer> le = new TreeNode<Integer>(5);
        TreeNode<Integer> ri = new TreeNode<Integer>(7);
        left.left = le;
        left.right = ri;

        TreeNode<Integer> r = new TreeNode<Integer>(15);
        TreeNode<Integer> l = new TreeNode<Integer>(11);
        right.left = l;
        right.right = r;


        TreeNode<Integer> root1 = new TreeNode<Integer>(10);
        root1.put(6);
        root1.put(12);
        root1.put(5);
        root1.put(7);
        root1.put(15);
        root1.put(11);
        //前序
        preIterateTreeNode(root);
        System.out.println();
        preIterateTreeNode(root1);
        
        System.out.println();
        System.out.println("中续");
        //中续
        midIterateTreeNode(root);
        System.out.println();
        midIterateTreeNode(root1);
        
        
        System.out.println();
        System.out.println("后续");
        //后续
        lastIterateTreeNode(root);
        System.out.println();
        lastIterateTreeNode(root1);
    }
    
    //前序遍历
    public void preIterateTreeNode(TreeNode<Integer> root){
        if(root == null){
            return;
        }
        System.out.print(root.value + ",");
        preIterateTreeNode(root.left);
        preIterateTreeNode(root.right);
    }
    //中序遍历
    public void midIterateTreeNode(TreeNode<Integer> root){
        if(root == null){
            return;
        }
        midIterateTreeNode(root.left);
        System.out.print(root.value+",");
        midIterateTreeNode(root.right);
    }
    //后序遍历
    public void lastIterateTreeNode(TreeNode<Integer> root){
        if(root == null){
            return;
        }
        midIterateTreeNode(root.left);
        midIterateTreeNode(root.right);
        System.out.print(root.value+",");
    }
}
